package com.janal.model.service.login;

import com.janal.model.dao.user.User;
import com.janal.model.dao.user.UserDao;
import com.janal.model.dao.user.UserDaoImpl;
import com.janal.util.utils;

import java.sql.Connection;
import java.sql.SQLException;

public class LoginServiceImpl implements LoginService{
    private UserDaoImpl userDao;

    public LoginServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(User user) {
        Connection conn = null;
        User resultuser = null;
        try {
            conn = utils.getConnection();
            resultuser = userDao.getLoginUser(conn,user.getUsername());
            if(resultuser != null){                        //若resultuser为空，说明用户不存在
                if(!resultuser.getPassword().equals(user.getPassword())){
                    resultuser.setPassword(null);          //密码不正确，置resultuser的密码为空
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            utils.close(null,conn);
        }
        return resultuser;
    }
}
